﻿using CodeGenerator.CodeGenBL;
using CodeGenerator.Models;
using CodeGenerator.ProjectBL;
using CodeGenerator.Template;
using CodeGenerator.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Generator
{
    internal class WebGenerator : GenerateBase
    {
        internal WebGenerator(IProjectBL projectBL, ICodeGenBL codeGenBL, String outputPath)
            : base(projectBL, codeGenBL, outputPath)
        {
             
        }

        public override void Generate(int projectId)
        {

            var projectDto = this.ProjectBL.GetProject(new ProjectBL.ProjectPkDto { ProjectId = projectId });
            var classDto = this.CodeGenBL.GetClassByProjectId(new CodeGenBL.ProjectPkDto { ProjectId = projectId });

            if (projectDto.Status && classDto.Status)
            {
                if (projectDto.Data != null && classDto.Data != null)
                {
                    ProjectModel projectModel = new ProjectModel(projectDto.Data);
                    List<ClassModel> classModels = new List<ClassModel>();
                    foreach (var classData in classDto.Data)
                    {
                        classModels.Add(new ClassModel(classData));
                    }
                    MetaData data = new MetaData() { Models = classModels, Project = projectModel };

                    OutputPath = Path.Combine(OutputPath, data.Project.ProjectName);
                    // Create Output Root Path if not exits (outputpath/projectname)
                    DirectoryExt.CreateIfMissing(OutputPath);

                    //Generate ไฟล์ tt 
                    GenerateControllers(data);
                    GenerateContent(data);
                    GeneratePopupController(data);
                    GenerateCustomHelpers(data);
                    GenerateMapper(data);
                    GeneratePicklistMapper(data);
                    GenerateModels(data);
                    GenerateListModel(data);
                    GenerateCustomListItem(data);
                    GenerateGetAllRequestModel(data);
                    GenerateGetAllResponseModel(data);
                    GenerateProperties(data);
                    GenerateUtilities(data);
                    GenerateAdd(data);
                    GenerateList(data);
                    GenerateViewDetail(data);
                    GeneratePopLookup(data);
                    GenerateShared(data);
                    GenerateWeb(data);
                }
            }
        }

        //generate file ที่อยู่ใน folder content โดยต้องไม่สร้าง folder แยกตาม ClassModel 
        private void GenerateContent(MetaData data)
        {
            var ttFiles = Directory.GetFiles("Template/Web/Content", "*.tt", SearchOption.TopDirectoryOnly); //Get file tt ทั้งหมดใน Folder Template/Web/Content
            var path = OutputPath + @"/{ProjectName}.Web/Content/";
            foreach (var filepath in ttFiles)
            {
                var filename = Path.GetFileNameWithoutExtension(filepath);

                //Generate in content Folder
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Content." + filename));
                itemp.Data.Models = data.Models;
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + itemp.Filename;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }

        }

        //Generate Controllers file
        private void GenerateControllers(MetaData data)
        {
            //var ttController = new CodeGenerator.Template.Web.Controllers.Controller();
            var path = OutputPath + @"/{ProjectName}.Web/Controllers/";
            foreach (var modelItem in data.Models)
            {
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Controllers.Controller"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                   File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));

             }
        }

        //Generate PopupController file
        private void GeneratePopupController(MetaData data)
        {
            //สร้างไฟล์ที่มีข้อมูลเป็น Lookup table ของทุก classModel ที่มี DataType เป็น Lookup Table
            List<LookupClassModel> lookupModels = new List<LookupClassModel>();
            var path = OutputPath + @"/{ProjectName}.Web/Controllers/";
            
            foreach (var modelItem in data.Models)
            {
                //วนเช็คว่ามี attribute ใดเป็น Lookup Table 
                foreach (var attributes in modelItem._attributes)
                {
                    if (attributes.DataType.Name == "Lookup Table")
                    {
                        var lookupDto = this.CodeGenBL.GetLookupClass(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (lookupDto.Data != null)
                        {
                            lookupModels.Add(new LookupClassModel(lookupDto.Data));
                        }
                    }
                }  
            }
            //เซตค่าให้ partail 
            var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
            DirectoryExt.CreateIfMissing(destinationPath);
            ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Controllers.PopupController"));
            itemp.Data.Models = data.Models;
            itemp.Data.Project = data.Project;
            itemp.lookups = lookupModels;
            itemp.pickuplist = null;
            var outputString = itemp.TransformText();
            var destinationFilePath = destinationPath + "/" + itemp.Filename;
            if (File.Exists(destinationFilePath))
            {
                File.Delete(destinationFilePath);
            }
            File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            
            
        }

        //Generate file in CustomHelpers
        private void GenerateCustomHelpers (MetaData data)
        {
            var ttFiles = Directory.GetFiles("Template/Web/CustomHelpers", "*.tt", SearchOption.TopDirectoryOnly); //Get file tt ทั้งหมดใน Folder Template/Web/Content
            var path = OutputPath + @"/{ProjectName}.Web/CustomHelpers/";
            foreach (var filepath in ttFiles)
            {
                var filename = Path.GetFileNameWithoutExtension(filepath);

                //Generate in content Folder
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.CustomHelpers." + filename));
                itemp.Data.Models = data.Models;
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + itemp.Filename;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
        }

        //Generate Mapper file
        private void GenerateMapper(MetaData data)
        {
            //สร้างไฟล์ที่มีข้อมูลเป็น Lookup table ของทุก classModel ที่มี DataType เป็น Lookup Table
            List<LookupClassModel> lookupModels = new List<LookupClassModel>();
            var path = OutputPath + @"/{ProjectName}.Web/Mapper/";

            foreach (var modelItem in data.Models)
            {
                //วนเช็คว่ามี attribute ใดเป็น Lookup Table 
                foreach (var attributes in modelItem._attributes)
                {
                    if (attributes.DataType.Name == "Lookup Table")
                    {
                        var lookupDto = this.CodeGenBL.GetLookupClass(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (lookupDto.Data != null)
                        {
                            lookupModels.Add(new LookupClassModel(lookupDto.Data));
                        }
                    }
                }
                //เซตค่าให้ partail 
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Mapper.Mapper"));
                itemp.Data.Models = new List<ClassModel> {modelItem};
                itemp.Data.Project = data.Project;
                itemp.lookups = lookupModels;
                itemp.pickuplist = null;
                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }
                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
            
            
        }

        //Generate PicklistMapper
        private void GeneratePicklistMapper(MetaData data)
        {
            //หลักการสร้างไฟล์ คือเช็คว่าในแต่ละ Attribute ของ ClassModel มี DataType เป็น Picklist
            var path = OutputPath + @"/{ProjectName}.Web/Mapper/";
            //List<PickupListModel> picklistModels = new List<PickupListModel>();
            List<PickupListModel> picklistModels = null;
            foreach (var modelItem in data.Models)
            {
                foreach (var attributes in modelItem._attributes)
                {
                    //if (attributes.DataType.Name == "Pickup List")
                    //{
                    //    var picklistDto = this.CodeGenBL.GetPickupList(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                    //    if (picklistDto.Data != null)
                    //    {
                    //        foreach (var picklist in picklistDto.Data)
                    //        {
                    //            picklistModels.Add(new PickupListModel(picklist, attributes.Id, attributes.Name));
                    //        }

                    //    }
                    //    //เซตค่าให้ partail 
                    //    var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                    //    DirectoryExt.CreateIfMissing(destinationPath);
                    //    ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Mapper.PicklistMapper"));
                    //    itemp.Data.Models = new List<ClassModel> { modelItem };
                    //    itemp.Data.Project = data.Project;
                    //    itemp.pickuplist = picklistModels;
                    //    itemp.lookups = null;

                    //    var filenameApplyFormat = itemp.Filename.Replace("{PicklistName}", attributes.Name);
                    //    var outputString = itemp.TransformText();
                    //    var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                    //    if (File.Exists(destinationFilePath))
                    //    {
                    //        File.Delete(destinationFilePath);
                    //    }
                    //    File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
                    //}
                    //if (picklistModels.Count > 0)
                    //{
                    //    picklistModels.RemoveAt(0);
                    //}

                    if (attributes.DataType.Name == "Pickup List")
                    {
                        var picklistDto = this.CodeGenBL.GetPickupList(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        picklistModels = new List<PickupListModel>();
                        if (picklistDto.Data != null)
                        {
                            foreach (var picklist in picklistDto.Data)
                            {
                                picklistModels.Add(new PickupListModel(picklist, attributes.Id, attributes.Name));
                            }

                        }
                        //เซตค่าให้ partail 
                        var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                        DirectoryExt.CreateIfMissing(destinationPath);
                        ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Mapper.PicklistMapper"));
                        itemp.Data.Models = new List<ClassModel> { modelItem };
                        itemp.Data.Project = data.Project;
                        itemp.pickuplist = picklistModels;
                        itemp.lookups = null;

                        var filenameApplyFormat = itemp.Filename.Replace("{PicklistName}", attributes.Name);
                        var outputString = itemp.TransformText();
                        var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                        if (File.Exists(destinationFilePath))
                        {
                            File.Delete(destinationFilePath);
                        }
                        File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
                    }
                    //if (picklistModels.Count > 0)
                    //{
                    //    picklistModels.RemoveAt(0);
                    //}
         
                }
            }

        }

        //Generate Models File
        private void GenerateModels(MetaData data)
        {
            List<LookupClassModel> lookupModels = new List<LookupClassModel>();
            List<PickupListModel> picklistModels = new List<PickupListModel>();
            var path = OutputPath + @"/{ProjectName}.Web/Models/";

            foreach (var modelItem in data.Models)
            {
                //วนเช็คว่ามี attribute ใดเป็น Lookup Table 
                foreach (var attributes in modelItem._attributes)
                {
                    if (attributes.DataType.Name == "Lookup Table")
                    {
                        var lookupDto = this.CodeGenBL.GetLookupClass(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (lookupDto.Data != null)
                        {
                            lookupModels.Add(new LookupClassModel(lookupDto.Data));
                        }
                    }
                    if (attributes.DataType.Name == "Pickup List")
                    {
                        var picklistDto = this.CodeGenBL.GetPickupList(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (picklistDto.Data != null)
                        {
                            foreach (var picklist in picklistDto.Data)
                            {
                                picklistModels.Add(new PickupListModel(picklist, attributes.Id, attributes.Name));
                            }

                        }
                    }
                }
                //เซตค่าให้ partail 
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Models.Model"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = lookupModels;
                itemp.pickuplist = picklistModels;

                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }
                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
            
        }

        //Generate ListModel file
        private void GenerateListModel(MetaData data)
        {
            
            var path = OutputPath + @"/{ProjectName}.Web/Models/";
            foreach (var modelItem in data.Models)
            {
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Models.ListModel"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));

            }
        }

        //Generate CustomListItem file
        private void GenerateCustomListItem(MetaData data)
        {
            var path = OutputPath + @"/{ProjectName}.Web/Models/";
            var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
            DirectoryExt.CreateIfMissing(destinationPath);
            ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Models.CustomListItem"));
            itemp.Data.Models = data.Models;
            itemp.Data.Project = data.Project;
            itemp.lookups = null;
            itemp.pickuplist = null;
            var outputString = itemp.TransformText();
            var destinationFilePath = destinationPath + "/" + itemp.Filename;
            if (File.Exists(destinationFilePath))
            {
                File.Delete(destinationFilePath);
            }

            File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));

        }

        //Generate GetAllRequestModel file
        private void GenerateGetAllRequestModel(MetaData data)
        {
            var path = OutputPath + @"/{ProjectName}.Web/Models/";
            foreach (var modelItem in data.Models)
            {
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Models.GetAllRequestModel"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));

            }
        }

        //Generate GetAllResponseModel file
        private void GenerateGetAllResponseModel(MetaData data)
        {
            var path = OutputPath + @"/{ProjectName}.Web/Models/";
            foreach (var modelItem in data.Models)
            {
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Models.GetAllResponseModel"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));

            }
        }

        //Generate in Properties Folder
        private void GenerateProperties(MetaData data)
        {
            //generate file ที่อยู่ใน folder Properties โดยต้องไม่สร้าง folder แยกตาม ClassModel 
            var ttFiles = Directory.GetFiles("Template/Web/Properties", "*.tt", SearchOption.TopDirectoryOnly); //Get file tt ทั้งหมดใน Folder Template/Web/Content
            var path = OutputPath + @"/{ProjectName}.Web/Properties/";
            foreach (var filepath in ttFiles)
            {
                var filename = Path.GetFileNameWithoutExtension(filepath);

                //******** อันนี้ไม่ต้อง foreach (var modelItem in _data.Models)
                //Generate in content Folder
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Properties." + filename));
                itemp.Data.Models = data.Models;
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                //var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + itemp.Filename;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
            
        }

        //Generate in Utilities Folder
        private void GenerateUtilities(MetaData data)
        {
            //generate file ที่อยู่ใน folder content โดยต้องไม่สร้าง folder แยกตาม ClassModel 
            var ttFiles = Directory.GetFiles("Template/Web/Utilities", "*.tt", SearchOption.TopDirectoryOnly); //Get file tt ทั้งหมดใน Folder Template/Web/Content
            var path = OutputPath + @"/{ProjectName}.Web/Utilities/";
            foreach (var filepath in ttFiles)
            {
                var filename = Path.GetFileNameWithoutExtension(filepath);

                //******** อันนี้ไม่ต้อง foreach (var modelItem in _data.Models)
                //Generate in Utilities Folder
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Utilities." + filename));
                itemp.Data.Models = null;
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + itemp.Filename;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
            
        }

        //Generate Add file in Views Folder
        private void GenerateAdd(MetaData data)
        {
            List<LookupClassModel> lookupModels = new List<LookupClassModel>();
            List<PickupListModel> picklistModels = new List<PickupListModel>();
            var path = OutputPath + @"/{ProjectName}.Web/Views/{ClassName}/";

            foreach (var modelItem in data.Models)
            {
                //วนเช็คว่ามี attribute ใดเป็น Lookup Table 
                foreach (var attributes in modelItem._attributes)
                {
                    if (attributes.DataType.Name == "Lookup Table")
                    {
                        var lookupDto = this.CodeGenBL.GetLookupClass(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (lookupDto.Data != null)
                        {
                            lookupModels.Add(new LookupClassModel(lookupDto.Data));
                        }
                    }
                    if (attributes.DataType.Name == "Pickup List")
                    {
                        var picklistDto = this.CodeGenBL.GetPickupList(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (picklistDto.Data != null)
                        {
                            foreach (var picklist in picklistDto.Data)
                            {
                                picklistModels.Add(new PickupListModel(picklist, attributes.Id, attributes.Name));
                            }

                        }
                    }
                }
                //เซตค่าให้ partail 
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Views.Add"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = lookupModels;
                itemp.pickuplist = picklistModels;

                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }
                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
            
        }

        //Generate List file in Views Folder
        private void GenerateList(MetaData data)
        {
            List<LookupClassModel> lookupModels = new List<LookupClassModel>();
            List<PickupListModel> picklistModels = new List<PickupListModel>();
            var path = OutputPath + @"/{ProjectName}.Web/Views/{ClassName}/";

            foreach (var modelItem in data.Models)
            {
                //วนเช็คว่ามี attribute ใดเป็น Lookup Table 
                foreach (var attributes in modelItem._attributes)
                {
                    if (attributes.DataType.Name == "Lookup Table")
                    {
                        var lookupDto = this.CodeGenBL.GetLookupClass(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (lookupDto.Data != null)
                        {
                            lookupModels.Add(new LookupClassModel(lookupDto.Data));
                        }
                    }
                    if (attributes.DataType.Name == "Pickup List")
                    {
                        var picklistDto = this.CodeGenBL.GetPickupList(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (picklistDto.Data != null)
                        {
                            foreach (var picklist in picklistDto.Data)
                            {
                                picklistModels.Add(new PickupListModel(picklist, attributes.Id, attributes.Name));
                            }

                        }
                    }
                }
                //เซตค่าให้ partail 
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Views.List"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = lookupModels;
                itemp.pickuplist = picklistModels;

                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }
                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
        }

        //Generate Add ViewDetail in Views Folder
        private void GenerateViewDetail(MetaData data)
        {
            List<LookupClassModel> lookupModels = new List<LookupClassModel>();
            List<PickupListModel> picklistModels = new List<PickupListModel>();
            var path = OutputPath + @"/{ProjectName}.Web/Views/{ClassName}/";

            foreach (var modelItem in data.Models)
            {
                //วนเช็คว่ามี attribute ใดเป็น Lookup Table 
                foreach (var attributes in modelItem._attributes)
                {
                    if (attributes.DataType.Name == "Lookup Table")
                    {
                        var lookupDto = this.CodeGenBL.GetLookupClass(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (lookupDto.Data != null)
                        {
                            lookupModels.Add(new LookupClassModel(lookupDto.Data));
                        }
                    }
                    if (attributes.DataType.Name == "Pickup List")
                    {
                        var picklistDto = this.CodeGenBL.GetPickupList(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (picklistDto.Data != null)
                        {
                            foreach (var picklist in picklistDto.Data)
                            {
                                picklistModels.Add(new PickupListModel(picklist, attributes.Id, attributes.Name));
                            }

                        }
                    }
                }
                //เซตค่าให้ partail 
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Views.ViewDetail"));
                itemp.Data.Models = new List<ClassModel> { modelItem };
                itemp.Data.Project = data.Project;
                itemp.lookups = lookupModels;
                itemp.pickuplist = picklistModels;

                var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }
                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }

        }

        //Generate Add PopLookup in Views Folder
        private void GeneratePopLookup(MetaData data)
        {
            List<LookupClassModel> lookupModels = new List<LookupClassModel>();
            var path = OutputPath + @"/{ProjectName}.Web/Views/Popup/";

            foreach (var modelItem in data.Models)
            {
                //วนเช็คว่ามี attribute ใดเป็น Lookup Table 
                foreach (var attributes in modelItem._attributes)
                {
                    if (attributes.DataType.Name == "Lookup Table")
                    {
                        var lookupDto = this.CodeGenBL.GetLookupClass(new CodeGenBL.AttributePkDto { Id = attributes.Id });
                        if (lookupDto.Data != null)
                        {
                            
                            lookupModels.Add(new LookupClassModel(lookupDto.Data));
                        }
                        //เซตค่าให้ partail 
                        var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
                        DirectoryExt.CreateIfMissing(destinationPath);
                        ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Views.PopLookup"));
                        itemp.Data.Models = new List<ClassModel> { modelItem };
                        itemp.Data.Project = data.Project;
                        itemp.lookups = lookupModels;
                        itemp.pickuplist = null;

                        var filenameApplyFormat = itemp.Filename.Replace("{ClassName}", attributes.Name);
                        var outputString = itemp.TransformText();
                        var destinationFilePath = destinationPath + "/" + filenameApplyFormat;
                        if (File.Exists(destinationFilePath))
                        {
                            File.Delete(destinationFilePath);
                        }
                        File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
                    }
                    if (lookupModels.Count > 0)
                    {
                        lookupModels.RemoveAt(0);
                    }
                }               
            }
        }

        //Generate _Layout file
        private void GenerateShared(MetaData data)
        {
            //generate file ที่อยู่ใน folder content โดยต้องไม่สร้าง folder แยกตาม ClassModel 
            var ttFiles = Directory.GetFiles("Template/Web/Views/Shared", "*.tt", SearchOption.TopDirectoryOnly); //Get file tt ทั้งหมดใน Folder Template/Web/Content
            var path = OutputPath + @"/{ProjectName}.Web/Views/Shared/";
            foreach (var filepath in ttFiles)
            {
                var filename = Path.GetFileNameWithoutExtension(filepath);

                //******** อันนี้ไม่ต้อง foreach (var modelItem in _data.Models)
                //Generate in Utilities Folder
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web.Views.Shared." + filename));
                itemp.Data.Models = data.Models;
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                //var filenameApplyFormat = dto.Filename.Replace("{ClassName}", modelItem.Name);
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + itemp.Filename;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }

        }

        //Generate in Web Folder
        private void GenerateWeb(MetaData data)
        {
            //generate file ที่อยู่ใน folder content โดยต้องไม่สร้าง folder แยกตาม ClassModel 
            var ttFiles = Directory.GetFiles("Template/Web", "*.tt", SearchOption.TopDirectoryOnly); //Get file tt ทั้งหมดใน Folder Template/Web/Content
            var path = OutputPath + @"/{ProjectName}.Web";
            foreach (var filepath in ttFiles)
            {
                var filename = Path.GetFileNameWithoutExtension(filepath);
                var destinationPath = path.Replace("{ProjectName}", data.Project.ProjectName);
                DirectoryExt.CreateIfMissing(destinationPath);
                ITemplate itemp = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template.Web." + filename));
                itemp.Data.Models = data.Models;
                itemp.Data.Project = data.Project;
                itemp.lookups = null;
                itemp.pickuplist = null;
                var outputString = itemp.TransformText();
                var destinationFilePath = destinationPath + "/" + itemp.Filename;
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }

                File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
            }
            
        }
    }
}
